package com.doctor.windflower_doctor.pop;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.DoctorGroupAction;
import com.doctor.windflower_doctor.activity.BaseActivity;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.delegate.WindflowerDelegate;
import com.doctor.windflower_doctor.entity.DoctorGroupBeen;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;

/**
 * <p>
 * 功能 修改备注
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年11月4日 下午1:56:59
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class TypeSelectPop extends BasePopupWindow implements Constent {
	private ListView type_list;
	private WindflowerDelegate windflowerDelegate;
	private LoadingDialog loadingDialog;
	private List<DoctorGroupBeen> doctorGroupBeens;
	private TypeSelectAdapter typeSelectAdapter;
	private String groupId;
	private RelativeLayout type_select;

	public TypeSelectPop(BaseActivity act, String userIds) {
		super(act, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, userIds);
	}

	@Override
	protected void onShowPre() {
		doctorGroupBeens = new ArrayList<DoctorGroupBeen>();
		loadingDialog = new LoadingDialog(mAct);
		super.onShowPre();

	}

	@Override
	protected void setListener() {
		type_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
				view.setSelected(true);
				ViewHolder holder = (ViewHolder) view.getTag();
				DoctorGroupBeen doctorGroupBeen = (DoctorGroupBeen) typeSelectAdapter.getItem(position);
				groupId = doctorGroupBeen._id;
				upDateGroup(holder, doctorGroupBeen);
			}
		});
		type_select.setOnClickListener(this);
	}

	@Override
	protected void findViews() {
		typeSelectAdapter = new TypeSelectAdapter();
		type_select = (RelativeLayout) findViewById(R.id.type_select);
		type_list = (ListView) findViewById(R.id.type_list);
		type_list.setAdapter(typeSelectAdapter);
		if (SystemApplication.getInstance().isTypeChange || SystemApplication.getInstance().doctorGroupBeens == null) {
			getList();
		} else {
			typeSelectAdapter.addListData(SystemApplication.getInstance().doctorGroupBeens);
		}
	}

	@Override
	protected int getContentViewLayoutId() {

		// TODO Auto-generated method stub
		return R.layout.type_select_pop;

	}

	@Override
	protected void initViewDrawableSelectCache() {

	}

	@Override
	protected void onItemClick(View view, PopupWindow pop) {
		pop.dismiss();
	}

	public void setWindflowerDelegate(WindflowerDelegate windflowerDelegate) {
		this.windflowerDelegate = windflowerDelegate;
	}

	private void getList() {
		HttpMethod requestMethod = HttpMethod.GET;
		String requestUrl = URL + GROUP_DOCTOR_GETALL + "?" + DOCTORID + "=" + SharedPreferencesDB.getInstance(mAct).getDocotorId();
		LogUtils.e(">>>>>>>>>requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<DoctorGroupAction> requestCallBack = new HttpRequestCallBack<DoctorGroupAction>(new JsonParser(), DoctorGroupAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<DoctorGroupAction> responseInfo) {
				loadingDialog.close();
				if (responseInfo.success.success) {
					if (responseInfo.success.data != null && responseInfo.success.data.groups != null)
						doctorGroupBeens = responseInfo.success.data.groups;
					if (doctorGroupBeens.size() > 0) {
						SystemApplication.getInstance().doctorGroupBeens = doctorGroupBeens;
						SystemApplication.getInstance().isTypeChange = false;
						typeSelectAdapter.addListData(doctorGroupBeens);
					}
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();
			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	class TypeSelectAdapter extends BaseAdapter {
		List<DoctorGroupBeen> doctorGroupBeens = new ArrayList<DoctorGroupBeen>();

		@Override
		public int getCount() {

			// TODO Auto-generated method stub
			return doctorGroupBeens.size();

		}

		@Override
		public Object getItem(int position) {

			// TODO Auto-generated method stub
			return doctorGroupBeens.get(position);

		}

		@Override
		public long getItemId(int position) {

			// TODO Auto-generated method stub
			return position;

		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(mAct).inflate(R.layout.item_select_type, null);
				holder.select_img = (ImageView) convertView.findViewById(R.id.select_img);
				holder.text_type = (TextView) convertView.findViewById(R.id.text_type);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.text_type.setText(doctorGroupBeens.get(position).groupName);
			return convertView;

		}

		public void addListData(List<DoctorGroupBeen> doctorGroupBeens) {
			if (doctorGroupBeens != null) {
				this.doctorGroupBeens.addAll(doctorGroupBeens);
				notifyDataSetChanged();
			}
		}

		public void clearData() {
			if (doctorGroupBeens != null) {
				this.doctorGroupBeens.clear();
				notifyDataSetChanged();
			}
		}
	}

	class ViewHolder {
		ImageView select_img;
		TextView text_type;
	}

	/**
	 * <p>
	 * 功能 更新分组
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月12日 上午10:08:56
	 */
	protected void upDateGroup(final ViewHolder holder, final DoctorGroupBeen doctorGroupBeen) {
		HttpMethod requestMethod = HttpMethod.POST;
		String requestUrl = URL + GROUP_DOCTOR_CHANGE;
		LogUtils.e(">>>>>>>>>requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<DoctorGroupAction> requestCallBack = new HttpRequestCallBack<DoctorGroupAction>(new JsonParser(), DoctorGroupAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<DoctorGroupAction> responseInfo) {
				loadingDialog.close();
				if (responseInfo.success.success) {
					LogUtils.e(">>>>>>>>>upDateGroup>>>>>>1>>>");
					holder.select_img.setVisibility(View.VISIBLE);
					windflowerDelegate.setId(doctorGroupBeen._id);
					windflowerDelegate.setName(doctorGroupBeen.groupName);
					mPop.dismiss();
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				LogUtils.e(">>>>>>>>>upDateGroup>>>>>>>>>" + msg);
				loadingDialog.close();
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();
			}

		};
		LogUtils.e(">>>>>>>>>mUserIds>>>>>>>>>" + mUserIds);
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		params.addBodyParameter(DOCTORID, SharedPreferencesDB.getInstance(mAct).getDocotorId());
		params.addBodyParameter(USERIDS, mUserIds);
		params.addBodyParameter(GROUPID, groupId);
		HttpRequest.send(params);
	}
}
